home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Chans / fax / dexNet200 / dexnet_conv.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  5.5 KB  |  240 lines

  1. /* dexnet_conv.c: convert g3 into Fujitsu */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Chans/fax/dexNet200/RCS/dexnet_conv.c,v 6.0 1991/12/18 20:09:42 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Chans/fax/dexNet200/RCS/dexnet_conv.c,v 6.0 1991/12/18 20:09:42 jpo Rel $
  9.  *
  10.  * $Log: dexnet_conv.c,v $
  11.  * Revision 6.0  1991/12/18  20:09:42  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. /* fujicnvrt.c     -- NASA Ames Research Center. 
  17.  
  18.         Author:        Jim Knowles
  19.         Internet:    jknowles@trident.arc.nasa.gov
  20.         Sterling Federal Systems
  21.         NAS2-13210
  22.         NASA, Ames Research Center
  23.         Mail Stop 233-18
  24.         Moffett Field CA 94035
  25.  
  26.  
  27.  Purpose:    encapsulates ASN.1 g3-encoded scanlines in FCRP -
  28.         Fujitsu proprietary command/response protocol.
  29.         resulting file can be transmitted by Fujitsu fax modem.
  30.         FCRP begins each scanline with ESC + and replaces G3 EOL
  31.         with NUL NUL after padding with zeros to byte-align.
  32.         this module is used by the FaxMail package.
  33.  
  34.  Input:        ASN.1 G3 encoded fax file received as faxmail enclosure.
  35.  
  36.  Output:    FCRP encapsulated G3 image file for transmittal.
  37.  
  38.  Errors:
  39.  
  40.  External references:
  41.     name        description            source
  42.     ----        -----------            ------
  43.  
  44.  Files:
  45.     name        description
  46.     ----        -----------
  47.             
  48.  
  49.  Environment:    4.3 BSD UNIX
  50.  
  51.  Development History:
  52.     date        initials    description
  53.     Apr  91        jdk        code developer.
  54. */
  55.  
  56.  
  57. #include <fcntl.h>
  58. #include <stdio.h>
  59. #ifdef sun
  60. #include <unistd.h>
  61. #else
  62. #ifdef vax
  63. #include <unistd.h>
  64. #else
  65. #define SEEK_SET 0
  66. #define SEEK_CUR 1
  67. #define SEEK_END 2
  68. #endif
  69. #endif
  70. #include <sys/types.h>
  71. #include <sys/file.h>
  72. #include <sysexits.h>
  73. #include <errno.h>
  74.  
  75. #define    MASK        0x80        /* initial mask for bit manipulation */
  76. #define BUF_SIZE    4096        /* buffer size */
  77.  
  78. #define TRUE            1               /* booleans */
  79. #define FALSE           0
  80. #define BOOL            int
  81. #define NUL             '\000'          /* nul char */
  82. #define ESC             '\033'          /* esc char */
  83. #define PLUS            '\053'          /* + char */
  84. #define FF        '\014'        /* form feed */
  85.  
  86.  
  87. /* forward declarations */
  88. static void output();
  89. static void set_bit();
  90. static void clr_bit();
  91.  
  92. /* global variables */
  93. static int fu_fd;                /* file descriptor for FCRP file */
  94. static int write_cnt;                /* characters written to write_buf */    
  95. static unsigned char g3_ch;            /* character in production */
  96. static unsigned char write_mask;        /* masked bit in char written */
  97. static unsigned char save_ch;            /* last character written */
  98. static unsigned char *buf_ptr;            /* pointer into write_buf */
  99.  
  100. /*
  101.  *    Convert each scanline to have escape + as prefix and NUL NUL as suffix
  102.  *    Return number of bytes placed into sink
  103.  */
  104. fujicnvrt(source, sink, length)
  105. unsigned char *source, *sink;
  106. int            length;    /* of source; sink assumed to be big enough */
  107. {
  108.     register unsigned char bit_mask;/* masks bit in currently read byte */
  109.     register unsigned char *ptr;    /* pointer into read buffer */
  110.     register unsigned char *eptr;    /* pointer to end of read buffer */
  111.     register int zero_bits = 0;    /* number of consecutive zero bits */
  112.     BOOL past_init = FALSE;        /* past the first eol sequence */
  113.     BOOL new_page = FALSE;        /* start on new page */
  114.     int    scanlines = 0;
  115.  
  116.     write_cnt = 0;                /* initialize for output */
  117.     write_mask = MASK;
  118.     buf_ptr = sink;                /* set start of write buffer */
  119.     zero_bits = 8;                /* initial NUL (part of eol) */
  120.     if (new_page) {
  121.         g3_ch = FF;
  122.         output(g3_ch);
  123.     }
  124.     output(ESC);
  125.     output(PLUS);
  126.     eptr = source + length;
  127.     for (ptr = source; ptr < eptr; ptr++) {
  128.         for (bit_mask = MASK; bit_mask > 0;) {
  129.             if (*ptr & bit_mask) {
  130.                 if (zero_bits >= 11) {
  131.                     zero_bits = 0;
  132.                     write_mask = MASK;
  133.                     if (past_init) {
  134.                         g3_ch ^= g3_ch;
  135.                         if (save_ch != g3_ch)
  136.                             output(g3_ch);
  137.                         output(g3_ch);
  138.                         output(ESC);
  139.                         output(PLUS);
  140.                         scanlines++;
  141.                     } else {
  142.                         past_init = TRUE;
  143.                         g3_ch ^= g3_ch;
  144.                     }
  145.                     if (bit_mask == 0) {
  146.                         bit_mask = MASK;
  147.                         if (++ptr == eptr)
  148.                             break;
  149.                     }
  150.                 } else {
  151.                     set_bit();
  152.                     zero_bits = 0;
  153.                 }
  154.             } else {
  155.                 if (past_init)
  156.                     clr_bit();
  157.                 zero_bits++;
  158.             }
  159.             bit_mask >>= 1;
  160.         }
  161.     }
  162.     while (write_mask != MASK)    /* write character in production */
  163.         clr_bit();
  164.     if (g3_ch) {
  165.         g3_ch ^= g3_ch;
  166.         output(g3_ch);
  167.     }
  168.     output(g3_ch);
  169. /*     fprintf(stderr, "%d scanlines\n", scanlines); */
  170.     return(write_cnt);
  171. }
  172.  
  173.  
  174.  
  175. /*++ output()
  176.  * function:    output one character into write buffer
  177.  * parameters:    o_ch - unsigned character to write into buffer.
  178.  * return:    none.
  179.  * global:    buf_ptr - pointer into write buffer.
  180.  *        save_ch - most recent character written.
  181.  *        write_cnt - number of characters written into write buffer.
  182.  *        write_buf - output buffer.
  183.  * called by:    main(), set_bit(), clear_bit().
  184.  * calls:    none.
  185.  --*/
  186.  
  187. static void
  188. output(o_ch)
  189. register unsigned char o_ch;
  190. {
  191.     *buf_ptr++ = save_ch = o_ch;
  192.     write_cnt++;
  193. }
  194.  
  195.  
  196. /*++ set_bit()
  197.  * function:    sets bit in current byte;
  198.  *        outputs to write buffer when byte is full.
  199.  * parameters:    none.
  200.  * return:    none.
  201.  * global:    write_mask - masks current bit in g3_ch.
  202.  *        g3_ch - character currently in production.
  203.  * called by:    main().
  204.  * calls:    output().
  205.  --*/
  206.  
  207. static void
  208. set_bit()
  209. {
  210.     g3_ch |= write_mask;
  211.     write_mask >>= 1;
  212.     if (write_mask == 0) {
  213.         output(g3_ch);
  214.         write_mask = MASK;
  215.     }
  216. }
  217.  
  218.  
  219. /*++ clr_bit()
  220.  * function:    clears bit in current byte;
  221.  *        outputs to write buffer when byte is full.
  222.  * parameters:    none.
  223.  * return:    none.
  224.  * global:    write_mask - masks current bit in g3_ch.
  225.  *        g3_ch - character currently in production.
  226.  * called by:    main().
  227.  * calls:    output().
  228.  --*/
  229.  
  230. static void
  231. clr_bit()
  232. {
  233.     g3_ch &= ~write_mask;
  234.     write_mask >>= 1;
  235.     if (write_mask == 0) {
  236.         output(g3_ch);
  237.         write_mask = MASK;
  238.     }
  239. }
  240.